<template>
  <div>
    <el-dialog title="提示" :visible.sync="dispathdialog" width="50%" :before-close="handleClose">
      <div>
        <p>用户的姓名为:{{ rightsuser.name }}</p>
        <p>用户的地址为:{{ rightsuser.address }}</p>
        <p>
          分配角色:<el-select v-model="selectrolesid" placeholder="请选择">
            <el-option v-for="item in setroles" :key="item.id" :label="item.roleName" :value="item.id"> </el-option>
          </el-select>
        </p>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closedispathdialog">取 消</el-button>
        <el-button type="primary" @click="setrights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import { dispathrights } from '@/api/index.js'
export default {
  data() {
    return {
      selectrolesid: ''
    }
  },
  computed: {
    ...mapState({
      dispathdialog: state => state.Users.dispathdialog,
      rightsuser: state => state.Users.rightsuser,
      setroles: state => state.Users.setroles
    })
  },
  methods: {
    ...mapMutations(['changedispathdialog', 'getusermenu']),
    closedispathdialog() {
      this.changedispathdialog(false)
      this.selectrolesid = ''
    },
    async setrights() {
      // console.log(1)
      if (!this.selectrolesid) {
        return this.$message.error('请选择角色')
      } else {
        const { data: res } = await dispathrights(this.rightsuser.id, { rid: this.selectrolesid })
        if (res.meta.status === 200) {
          this.$message.success(res.meta.msg)
          this.getusermenu()
          this.closedispathdialog()
        } else {
          this.$message.error(res.meta.msg)
        }
        // console.log(res)
      }
    },
    handleClose() {
      this.$confirm('确认关闭？')
        .then(_ => {
          // this.changedispathdialog(false)
          this.closedispathdialog()
          // console.log(this.selectrolesid)
        })
        .catch(_ => {})
    }
  }
}
</script>

<style></style>
